﻿import Vue from 'vue';
import router from 'vue-router';
import axios from 'axios';
Vue.use(router);

  const routers =new router({
  // export default new router({
      routes: [
          // 路由重定向功能
          {
            path: '/',
            name:'login',
            component: resolve => require(['../components/login.vue'], resolve)
          },
          {
            path: '/Layout',
            name:'Layout',
            component: resolve => require(['../components/layout.vue'], resolve),
            children: [
              {
                path: '/ChangePassword',//修改密码
                name:'ChangePassword',
                component: resolve => require(['../components/ChangePassword.vue'], resolve)
              },
              {
                path: '/home',//平台首页
                name:'home',
                component: resolve => require(['../components/terraceContent/home.vue'], resolve)
              },
              {
                path: '/policyService',//政策服务
                name:'policyService',
                component: resolve => require(['../components/terraceContent/policyService.vue'], resolve)
              },
              {
                path: '/policyExplain',//政策解读
                name:'policyExplain',
                component: resolve => require(['../components/terraceContent/policyExplain.vue'], resolve)
              },
              {
                path: '/enterpriseInfo',//企业信息
                name:'enterpriseInfo',
                component: resolve => require(['../components/terraceContent/enterpriseInfo.vue'], resolve)
              },
              {
                path: '/statistAnalysis',//统计分析
                name:'statistAnalysis',
                component: resolve => require(['../components/terraceContent/statistAnalysis.vue'], resolve)
              }
            ]
          }
        ]
  })

//全局路由守卫
routers.beforeEach(function (to, from, next) {
  const nextRoute = ['ChangePassword', 'home','policyService', 'policyExplain','enterpriseInfo', 'enterpriseInfo','statistAnalysis'];
  if (nextRoute.indexOf(to.name) >= 0) {
    let userName = sessionStorage.getItem("userName");
    console.log(userName);
    if(!userName){
      window.location.href='/login';
      // sessionStorage.clear();
    }
  };
  //已登录的情况再去登录页，跳转至首页
  if (sessionStorage.getItem('userName')) {
    next();
  };
  next();
});
export default routers;
